package jnpf.service.impl;

import jnpf.entity.Whu_shock_eventEntity;
import jnpf.mapper.Whu_shock_eventMapper;
import jnpf.service.Whu_shock_eventService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.util.RandomUtil;


import jnpf.model.whu_shock_event.Whu_shock_eventPagination;

import java.util.stream.Collectors;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.SimpleDateFormat;

import jnpf.util.*;

import java.util.*;

/**
 * whu_shock_event
 * 版本： V3.2.0
 * 版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * 作者： JNPF开发平台组
 * 日期： 2024-09-26
 */
@Service

public class Whu_shock_eventServiceImpl extends ServiceImpl<Whu_shock_eventMapper, Whu_shock_eventEntity> implements Whu_shock_eventService {

    @Autowired
    private UserProvider userProvider;


    @Override
    public List<Whu_shock_eventEntity> getList(Whu_shock_eventPagination whu_shock_eventPagination) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<Whu_shock_eventEntity> whu_shock_eventQueryWrapper = new QueryWrapper<>();
        int whu_shock_eventNum = 0;
        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getCode())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().like(Whu_shock_eventEntity::getCode, whu_shock_eventPagination.getCode());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getNumber())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().like(Whu_shock_eventEntity::getNumber, whu_shock_eventPagination.getNumber());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getType())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().eq(Whu_shock_eventEntity::getType, whu_shock_eventPagination.getType());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getAisle())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().eq(Whu_shock_eventEntity::getAisle, whu_shock_eventPagination.getAisle());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getTime())) {
            whu_shock_eventNum++;
            List<String> TimeList = whu_shock_eventPagination.getTime();
            Long fir = Long.valueOf(TimeList.get(0));
            Long sec = Long.valueOf(TimeList.get(1));

            whu_shock_eventQueryWrapper.lambda().ge(Whu_shock_eventEntity::getTime, new Date(fir))
                    .le(Whu_shock_eventEntity::getTime, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getState())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().eq(Whu_shock_eventEntity::getState, whu_shock_eventPagination.getState());
        }

        if (AllIdList.size() > 0) {
            whu_shock_eventQueryWrapper.lambda().in(Whu_shock_eventEntity::getId, AllIdList);
        }

        whu_shock_eventQueryWrapper.lambda().orderByDesc(Whu_shock_eventEntity::getTime);
        //排序
//        if (StringUtil.isEmpty(whu_shock_eventPagination.getSidx())) {
//            whu_shock_eventQueryWrapper.lambda().orderByDesc(Whu_shock_eventEntity::getId);
//        } else {
//            whu_shock_eventQueryWrapper = "asc".equals(whu_shock_eventPagination.getSort().toLowerCase()) ? whu_shock_eventQueryWrapper.orderByAsc(whu_shock_eventPagination.getSidx()) : whu_shock_eventQueryWrapper.orderByDesc(whu_shock_eventPagination.getSidx());
//        }
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<Whu_shock_eventEntity> page = new Page<>(whu_shock_eventPagination.getCurrentPage(), whu_shock_eventPagination.getPageSize());
            IPage<Whu_shock_eventEntity> userIPage = this.page(page, whu_shock_eventQueryWrapper);
            return whu_shock_eventPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<Whu_shock_eventEntity> list = new ArrayList();
            return whu_shock_eventPagination.setData(list, list.size());
        }
    }

    @Override
    public List<Whu_shock_eventEntity> getTypeList(Whu_shock_eventPagination whu_shock_eventPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<Whu_shock_eventEntity> whu_shock_eventQueryWrapper = new QueryWrapper<>();
        int whu_shock_eventNum = 0;
        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getCode())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().like(Whu_shock_eventEntity::getCode, whu_shock_eventPagination.getCode());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getNumber())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().like(Whu_shock_eventEntity::getNumber, whu_shock_eventPagination.getNumber());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getType())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().eq(Whu_shock_eventEntity::getType, whu_shock_eventPagination.getType());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getAisle())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().eq(Whu_shock_eventEntity::getAisle, whu_shock_eventPagination.getAisle());
        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getTime())) {
            whu_shock_eventNum++;
            List<String> TimeList = whu_shock_eventPagination.getTime();
            Long fir = Long.valueOf(TimeList.get(0));
            Long sec = Long.valueOf(TimeList.get(1));

            whu_shock_eventQueryWrapper.lambda().ge(Whu_shock_eventEntity::getTime, new Date(fir))
                    .le(Whu_shock_eventEntity::getTime, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(whu_shock_eventPagination.getState())) {
            whu_shock_eventNum++;
            whu_shock_eventQueryWrapper.lambda().eq(Whu_shock_eventEntity::getState, whu_shock_eventPagination.getState());
        }

        if (AllIdList.size() > 0) {
            whu_shock_eventQueryWrapper.lambda().in(Whu_shock_eventEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(whu_shock_eventPagination.getSidx())) {
            whu_shock_eventQueryWrapper.lambda().orderByDesc(Whu_shock_eventEntity::getId);
        } else {
            whu_shock_eventQueryWrapper = "asc".equals(whu_shock_eventPagination.getSort().toLowerCase()) ? whu_shock_eventQueryWrapper.orderByAsc(whu_shock_eventPagination.getSidx()) : whu_shock_eventQueryWrapper.orderByDesc(whu_shock_eventPagination.getSidx());
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<Whu_shock_eventEntity> page = new Page<>(whu_shock_eventPagination.getCurrentPage(), whu_shock_eventPagination.getPageSize());
                IPage<Whu_shock_eventEntity> userIPage = this.page(page, whu_shock_eventQueryWrapper);
                return whu_shock_eventPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<Whu_shock_eventEntity> list = new ArrayList();
                return whu_shock_eventPagination.setData(list, list.size());
            }
        } else {
            return this.list(whu_shock_eventQueryWrapper);
        }
    }

    @Override
    public Whu_shock_eventEntity getInfo(String id) {
        QueryWrapper<Whu_shock_eventEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Whu_shock_eventEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(Whu_shock_eventEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, Whu_shock_eventEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public int yesterdayAdd() {
        int result1 = this.baseMapper.selectYesterday();
        int result2 = this.baseMapper.selectToday();
        return result2 - result1;
    }

    @Override
    public void delete(Whu_shock_eventEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}